<?php
/**
 * Created by cmb_weixin.master.
 * File: ProductOrderReserve.php
 * User: xuewl
 * Date: 2017/12/20
 * Time: 00:35
 */

namespace app\product\service;
use app\common\library\Service;
use think\Hook;

class ProductOrderReserve extends Service
{

    use \app\common\library\traits\Model;

    protected $model;

    public function _initialize() {
        parent::_initialize();
        $this->model = model('product/product_order_reserve');
    }

    /**
     * 客户经理抢单
     * @param $order_sn
     * @param $customer_id
     * @return boolean
     */
    public function create($order_sn, $customer_id) {
        $product_order = model("product/product_order", 'service')->where(['sn' => $order_sn])->find();
        if(!$product_order) {
            $this->error = model("product/product_order", 'service')->getError();
            return false;
        }

        $customer = model("user/user", "service")
            ->where([
                'id' => $customer_id,
                'role' => 2,
                'status' => 1,
            ])
            ->find();

        if(!$customer) {
            $this->error = "客户经理信息异常，请联系管理员";
            return false;
        }

        // 禁止重复抢单
        $exists = $this->where([
            'order_sn' => $order_sn,
            'customer_id' => $customer_id
        ])->count();

        if($exists > 0) {
            $this->error = "您已抢单，不能重复抢单";
            return false;
        }

        $info = [
            "order_sn" => $order_sn,
            "product_id" => $product_order['product_id'],
            "order_uid" => $product_order['uid'],
            "customer_id" => $customer_id,
            "weight" => 0
        ];

        \think\Db::startTrans();

        $result = $this->save($info, [], 'add');
        if(!$result) {
            $this->error = $this->getError();
            return false;
        }

        // 更新首次抢单时间
        if($product_order['reserve_time'] == 0) {
//            $order = ['reserve_time'];
            $product_order_result = \think\Db::name("product_order")
                ->where(['id' => $product_order['id']])
                ->update(['reserve_time' => time()]);
            if($product_order_result === 0) {
                \think\Db::rollback();
                $this->error = "网络繁忙";
                return false;
            }
        }

        \think\Hook::listen("ProductOrderReserveCreateAfter", $result);
        \think\Db::commit();
        return $result;
    }

    /**
     * 抢单结果审批
     * @param string $sn
     * @param integer $customer_id
     * @return mixed
     */
    public function review($sn, $customer_id = 0) {
        if($customer_id == 0) {
            $customer_id = $this->model->where([
                'order_sn' => $sn,
                'result' => 0,
            ])->order('weight desc')->value('customer_id');
        }

        if(is_null($customer_id)) {
            $this->error = '没有抢单记录';
            return false;
        }


        // 查询订单状态
        $order = model('product/product_order', 'service')->where([
            'sn' => $sn,
            'customer_id' => 0,
        ])->find();
        if(!$order) {
            $this->error = '订单状态异常';
            return false;
        }
        $order->customer_id = $customer_id;
        \think\Db::startTrans();
        $result = $order->save();
        if(!$result) {
            $this->error = '订单抢单失败';
            return false;
        }
        // 更新抢单记录表
        $this->save(['result' => -1], [
            'order_sn' => $sn,
            'customer_id' => ['neq', $customer_id]
        ]);
        $this->save(['result' => 1], [
            'order_sn' => $sn,
            'customer_id' => ['eq', $customer_id]
        ]);
        \think\Hook::listen("ProductOrderReserveReviewAfter", $order);
        \think\Db::commit();
        return $order;
    }

}